package com.huawei.smartcity0102.util;
import android.util.Log;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * @author ZengYunyi
 * @description:
 * @date :2021/12/27 14:11
 */
public class Http {
    public static final Gson g=new Gson();
    private static final OkHttpClient o=new OkHttpClient();
    public static final MediaType m=MediaType.parse("application/json");
    public static String base="http://121.9.253.236:10001";
    public static String token;

    private static final class S{
        private static final Http h=new Http();
    }

    public static final Http getInstance(){
        return S.h;
    }

    public <T> T get(String url,RequestBody body,Class<T> tClass,int i){
        if(token==null)token="";
        url=base+url;
        Log.i("ddddd", url);
        try {
            if(i==1) return g.fromJson(o.newCall(new Request.Builder().url(url).addHeader("Authorization",token).get().build()).execute().body().string(),tClass);
            if(i==2) return g.fromJson(o.newCall(new Request.Builder().url(url).addHeader("Authorization",token).post(body).build()).execute().body().string(),tClass);
            if(i==3) return g.fromJson(o.newCall(new Request.Builder().url(url).addHeader("Authorization",token).put(body).build()).execute().body().string(),tClass);
            if(i==4) return g.fromJson(o.newCall(new Request.Builder().url(url).addHeader("Authorization",token).delete().build()).execute().body().string(),tClass);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}